package com.wzy.spring2.controller;


import com.wzy.spring2.dao.WebUserDao;
import com.wzy.spring2.model.QueryCondition;
import com.wzy.spring2.model.WebUser;
import com.wzy.spring2.service.AccountStoreService;
import com.wzy.tools.utils.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Weizehngyun
 * @date 2022/12/17 上午9:36
 */
@RestController
@RequestMapping("account")
@Slf4j
public class AccountController {

    @Autowired
    private AccountStoreService accountStoreService;

    @Autowired
    private WebUserDao dao;

    @RequestMapping(value = "get", method = {RequestMethod.GET})
    public String get(@RequestParam(value = "name", defaultValue = "", required = false) String name) {
        log.info("[get] AccountController. name = {}", name);
        QueryCondition qc = QueryCondition.builder().build();
        if (StringUtils.isNotBlank(name)) {
            qc.setWebName(name);
        }
        List<WebUser> webUserList = dao.select(qc);
        return JSON.toJSONString(webUserList);
    }

    @RequestMapping(value = "set", method = {RequestMethod.POST})
    public String set(@RequestParam("webName") String webName, @RequestParam("account") String account, @RequestParam("password") String passwd) {
        WebUser user = new WebUser();
        user.setAccount(account);
        user.setWebName(webName);
        user.setPassword(passwd);
        int res = dao.insert(user);
        if (res > 0) {
            return "成功";
        }
        return "失败";
    }

    @RequestMapping(value = "getAll", method = {RequestMethod.GET})
    public String get() {
        log.info("[getAll] AccountController");
        return JSON.toJSONString(AccountStoreService.accountMap);
    }

}
